package com.chenkun.controller.article;

import com.chenkun.common.define.CommRes;
import com.chenkun.model.Article;
import com.chenkun.model.RssEntry;
import com.chenkun.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.net.MalformedURLException;
import java.util.List;

/**
 * Created by chenkun on 2017/5/8.
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取所有文章列表
     * @return
     */
    @RequestMapping("/list")
    public @ResponseBody CommRes getAllArticle(){
        List<Article> articles = this.articleService.getAll();
        if (articles.size()==0){
            return CommRes.error("500","无数据");
        }
        return CommRes.success(articles);
    }

    /**
     * 添加新文章
     * @param article
     * @return
     */
    @RequestMapping("/add")
    public @ResponseBody CommRes add(@RequestBody Article article){
        int res = this.articleService.add(article);
        if (res<=0){
            return CommRes.error("500","添加失败");
        }
        return CommRes.success();
    }

    /**
     * 删除文章
     * @param code
     * @return
     */
    @RequestMapping("/delete/{code}")
    public @ResponseBody CommRes delete(@PathVariable("code") String code){
        int res = this.articleService.delete(code);
        if (res<=0){
            return CommRes.error("500","删除失败");
        }
        return CommRes.success();
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @RequestMapping("/update")
    public @ResponseBody CommRes update(@RequestBody Article article){
        int res = this.articleService.update(article);
        if (res<=0){
            return CommRes.error("500","更新失败");
        }
        return CommRes.success();
    }

    /**
     * 根据code获取文章信息
     * @param code
     * @return
     */
    @RequestMapping("/getByCode/{code}")
    public @ResponseBody CommRes getByCode(@PathVariable("code") String code){
        Article article = this.articleService.getByCode(code);
        if (article == null){
            return CommRes.error("500","查询失败");
        }
        return CommRes.success(article);
    }

    /**
     * solr全局搜索
     * @param keywords
     * @return
     */
    @RequestMapping("/solr")
    public @ResponseBody CommRes getByKeyWord(String keywords){
        List<Article> articles = this.articleService.getByKeyWords(keywords);
        if (articles.size()==0){
            return CommRes.error("500","查询结果为空");
        }
        return CommRes.success(articles);
    }

    /**
     * rss订阅
     * @param address
     * @return
     * @throws MalformedURLException
     */
    @RequestMapping("/rss-get")
    public @ResponseBody CommRes rssGet(String address) throws MalformedURLException {
        List<RssEntry> res = this.articleService.getRss(address);
        if (res==null||res.size()==0){
            return CommRes.error("500","解析失败");
        }
        return CommRes.success(res);
    }

    /**
     * rss发布
     * @return
     */
    @RequestMapping("/get-rss")
    public String createXml(){
        return this.articleService.createXml();
    }
}
